#include <iostream>
using namespace std;
void swap(int &a, int &b)
{
	int temp=a;
	a=b;
	b=temp;
}

void heapdajust(int *a, int i, int size)
{
	int lchild=i*2;
	int rchild=i*2+1;
	int max=i;
	if (i<=size/2)
	{
		if (lchild<=size&&a[lchild]>a[i])
		{
			i=lchild;
		}
		if (rchild<=size&&a[rchild]>a[i])
		{
			i=rchild;
		}
		if (i!=max)
		{
			swap(a[i],a[max]);
			heapdajust(a,i,size);
		}
	}

}

void createheap(int *a, int size)
{
	int i=size/2;
	for (i;i>=1;i--)
	{
		heapdajust(a,i,size);
	}
	//for (int i=1;i<=size/2;i++)
	//{
	//	heapdajust(a,i,size);
	//}
}

void heapsort(int *a, int size)
{
	//createheap(a,size);
	for (int i=size;i>=1;i--)
	{
		swap(a[1],a[i]);
		heapdajust(a,1,i-1);
	}
}



int main()
{
	int a[]={0,3,3,5,7,9,1,4,2,8,45};
	createheap(a,10);
	//heapsort(a,10);
	for (int i=1;i<=10;i++)
	{
		cout<<a[i]<<" ";
	}

	cout<<endl;
	/*int a[]={3,2,1,78,45,6,8,34,5,99,34,67,22,7};
	int b[4]={0};
	for (int i=1;i<4;i++)
	{
		b[i]=a[i];
	}
	createheap(b,3);
	for (int i=3;i<14;i++)
	{
		if (a[i]<b[1])
		{
			b[1]=a[i];
			createheap(b,3);
		}
	}
	heapsort(b,3);
	for (int k=1;k<4;k++)
	{
		cout<<b[k]<<" ";
	}*/
	return 0;
}